/**
 */
import AxiosInst from './axios';
import methodMap from './methodMap';

class Http {
}

/**
 * 注册VUE插件
 * @param Vue
 */
Http.install = function (Vue) {
  /**
   * 全局请求接口
   * @param method 方法
   * @param opts 参数
   * @param toast 是否提示
   * @returns {string}
   */
  Vue.prototype.request = function (method, opts, params, toast) {
    //如果有给 toast 参数则显示 loading 加载数据
    if (toast && typeof (toast) === 'boolean') {
      Vue.prototype.$loading('加载中...');
    } else if (toast && typeof (toast) === 'string') {
      Vue.prototype.$loading(toast);
    }
    let m = methodMap[method];
    if (m) {
      let optsType = typeof (opts);
      if (optsType === null || optsType !== 'object') {
        opts = {};
      }
      if (typeof m.method === 'undefined') {
        console.log('method 错误', '缺少请求 method 方法', '\n');
        return false;
      }
      if (m.method === 'get') {
        return Vue.prototype.apiGet(m.url, opts);
      } else if (m.method === 'post') {
        return Vue.prototype.apiPost(m.url, opts, params);
      } else if (m.method === 'export') {
        return Vue.prototype.apiExportPost(m.url, opts, params);
      } else {
        return false;
      }
    } else {
      closeLoading();
      console.log('url 错误', '返回结果：err = ', '无法请求，无效的请求！', '\n');
    }
  };
  /**
   * POST 请求 无提示
   * @param url 请求URL
   * @param data 请求数据
   * @returns {Promise}
   */
  Vue.prototype.apiPost = function (url, data, params, toast = false) {
    if (toast && typeof (toast) === 'boolean') {
      Vue.prototype.$loading('加载中...');
    } else if (toast && typeof (toast) === 'string') {
      Vue.prototype.$loading(toast);
    }
    return new Promise((resolve, reject) => {
      AxiosInst.post(url, data, {
        params: params
      }).then((response) => {
        setTimeout(() => closeLoading(), 800);
        console.log(response);
        resolve(response.data);
      }).catch((error) => {
        doErrInfo(error, this);
        closeLoading();
        reject(error);
      });
    });
  };
  /**
   * POST 请求 无提示
   * @param url 请求URL
   * @param data 请求数据
   * @returns {Promise}
   */
  Vue.prototype.apiExportPost = function (url, data, params, toast = false) {
    if (toast && typeof (toast) === 'boolean') {
      Vue.prototype.$loading('加载中...');
    } else if (toast && typeof (toast) === 'string') {
      Vue.prototype.$loading(toast);
    }
    return new Promise((resolve, reject) => {
      AxiosInst.post(url, data, {
        responseType: 'blob'
      }).then((response) => {
        setTimeout(() => closeLoading(), 800);
        resolve(response.data);

        const link = document.createElement('a');
        let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
        link.style.display = 'none';
        link.href = URL.createObjectURL(blob);

        link.download = params;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      }).catch((error) => {
        doErrInfo(error, this);
        closeLoading();
        reject(error);
      });
    });
  };
  /**
   * GET 请求 无提示
   * @param url 请求URL
   * @param data 请求数据
   * @returns {Promise}
   */
  Vue.prototype.apiGet = function (url, data, toast = false) {
    if (toast && typeof (toast) === 'boolean') {
      Vue.prototype.$loading('加载中...');
    } else if (toast && typeof (toast) === 'string') {
      Vue.prototype.$loading(toast);
    }
    return new Promise((resolve, reject) => {
      AxiosInst.get(url, {
        params: data
      }).then((response) => {
        setTimeout(() => closeLoading(), 800);
        resolve(response.data);
      }).catch((error) => {
        doErrInfo(error, this);
        closeLoading();
        reject(error);
      });
    });
  };

  /**
   * 错误处理
   * @param error 错误对象
   * @param obj vue统一对象，为了弹框
   */
  function doErrInfo(error, obj) {
    if (error.response === null || error.response === undefined) {
      return;
    }
    if (error.response.status >= 600) {
      if (error.response.data !== '' || error.response.data !== null) {
        //根据业务进行弹框输出
        if (error.response.data.msg !== undefined) {
          obj.$Message.error(error.response.data.msg);
        } else {
          obj.$Message.error(error.response.data);
        }
      } else {
        console.log('接口输出无数据', error.response);
      }
    } else if (error.response.status >= 200 && error.response.status < 300) {
      if (error.response.data !== '' || error.response.data !== null) {
        obj.$Notice.error({title: '接口输出异常...'});
        console.log(error.data);
      } else {
        console.log('接口输出无数据', error.response);
      }
    } else if (error.response.status === 500) {
      obj.$Notice.error({title: '后端服务请求500错误,如一直出现错误,请联系我们'});
      console.log(error.response);
    } else if (error.response.status > 500 && error.response.status < 600) {
      obj.$Notice.error({title: '服务请求超时，请联系我们'});
    } else {
      obj.$Notice.error({title: '服务请求出错'});
      console.log('Customize Notice', error);
    }
  }

  /**
   * 关闭方法
   */
  function closeLoading() {
    Vue.prototype.$loading.close();
  }
};
export default Http;
